package com.ptrans.server;

import static com.ptrans.utilities.CommonUtilities.*;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Node;

import android.content.Context;
import android.util.Log;

import com.google.android.gcm.GCMRegistrar;
import com.ptrans.R;
import com.ptrans.modele.User;
import com.ptrans.utilities.ParserXML;

/**
 * Class giving methods for registering, connecting, disconnecting,... user.
 * @author Jean
 *
 */
public class RegisterHandler {
    
	/**
	 * Send an http request by GET method for registering user on server
	 * @param context
	 * @param user
	 * @return
	 */
    public static boolean register(final Context context, User user) {
        Log.i(TAG, "registering user (name = " + user.getName() + ")");
        boolean b = false;
        String serverUrl = SERVER_URL;
        Node response;
        String message;
        Map<String, String> params = new HashMap<String, String>();
        params.put(PARAM_CONTROLLER, C_REGISTER);
        params.put(PARAM_ACTION, A_REGISTRATION);
        params.put(PARAM_USER_NAME, user.getName());

        response = ServerHttp.tryAConnection(serverUrl, params, context);
        if(response != null) {       
	        if(!ParserXML.getState(response).equals("")) {
	        	GCMRegistrar.setRegisteredOnServer(context, true);
	            message = context.getString(R.string.server_registered);
	            b = true;
	        } else {
	        	message = context.getString(R.string.server_unregistered);
	        }
	        Log.i(TAG, message);
	        return b;
        }
        
        message = context.getString(R.string.server_register_error);
        Log.i(TAG, message);
        return b;
    }
    
    /**
     * Send an Http request by Get method for unregistering user on server
     * @param context
     * @param user
     * @return
     */
    public static boolean unregister(final Context context, User user) {
        Log.i(TAG, "unregistering user (name = " + user.getName() + ")");
        boolean b = false;
        String serverUrl = SERVER_URL;
        Node response;
        String message;
        Map<String, String> params = new HashMap<String, String>();
        params.put(PARAM_CONTROLLER, C_REGISTER);
        params.put(PARAM_ACTION, A_UNREGISTRATION);
        params.put(PARAM_USER_NAME, user.getName());

        response = ServerHttp.tryAConnection(serverUrl, params, context);
        if(response != null) {       
	        if(!ParserXML.getState(response).equals("")) {
	        	GCMRegistrar.setRegisteredOnServer(context, true);
	            message = context.getString(R.string.server_unregistered);
	            b = true;
	        } else {
	        	message = context.getString(R.string.server_unregistered_error);
	        }
	        Log.i(TAG, message);
	        return b;
        }
        
        message = context.getString(R.string.server_register_error);
        Log.i(TAG, message);
        return b;
    }
    
    /**
     * Send an Http request by Post method for uploading picture on server
     * @param context
     * @param user
     * @return
     */
    public static boolean uploadUserPicture(final Context context, User user) {
        Log.i(TAG, "uploading img user (picture = " + user.getPicture() + ")");
        boolean b = false;
        Node response;
        String message;
        response = ServerHttp.uploadPicture(user.getPicture(), user.getName());
        if(response != null) {       
	        if(!ParserXML.getState(response).equals("")) {
	        	GCMRegistrar.setRegisteredOnServer(context, true);
	            message = context.getString(R.string.img_uploaded);
	            b = true;
	        } else {
	        	message = context.getString(R.string.img_uploaded_error);
	        }
	        Log.i(TAG, message);
	        return b;
        }
        
        message = context.getString(R.string.img_uploaded_error);
        Log.i(TAG, message);
        return b;
    }
    
    /**
     * Send an Http request by Get method for connecting user.
     * @param context
     * @param user
     * @return
     */
    public static boolean connect(final Context context, User user) {
        Log.i(TAG, "connecting device (regId = " + user.getRegId() + ")");
        boolean b = false;
        String serverUrl = SERVER_URL;
        Node response;
        String message;
        Map<String, String> params = new HashMap<String, String>();
        params.put(PARAM_CONTROLLER, C_REGISTER);
        params.put(PARAM_ACTION, A_CONNECT);
        params.put(PARAM_REGID, user.getRegId());
        params.put(PARAM_USER_NAME, user.getName());
        
        response = ServerHttp.tryAConnection(serverUrl, params, context);
        
        if(response != null) {       
	        if(!ParserXML.getState(response).equals("")) {
	        	GCMRegistrar.setRegisteredOnServer(context, true);
	            message = context.getString(R.string.user_connected);
	            b = true;
	        } else {
	        	message = context.getString(R.string.user_connection_error);
	        }
	        Log.i(TAG, message);
	        return b;
        }
        message = context.getString(R.string.user_connection_error);
        Log.i(TAG, message);
        return b;
    }
    
    /**
     * Send an Http request by Get method for disconnecting user.
     * @param context
     * @param user
     * @return
     */
    public static boolean disconnect(final Context context, User user) {
        Log.i(TAG, "disconnecting device (regId = " + user.getRegId() + ")");
        boolean b = false;
        String serverUrl = SERVER_URL;
        Node response;
        String message;
        Map<String, String> params = new HashMap<String, String>();
        params.put(PARAM_CONTROLLER, C_REGISTER);
        params.put(PARAM_ACTION, A_DISCONNECT);
        params.put(PARAM_REGID, user.getRegId());
        params.put(PARAM_USER_NAME, user.getName());
        
        response = ServerHttp.tryAConnection(serverUrl, params, context);
        
        if(response != null) {
	        if(!ParserXML.getState(response).equals("")) {
	        	GCMRegistrar.setRegisteredOnServer(context, true);
	            message = context.getString(R.string.user_disconnected);
	            b = true;
	        } else {
	        	message = context.getString(R.string.user_disconnection_error);
	        }
	        Log.i(TAG, message);
	        return b;
    	}
        
        message = context.getString(R.string.user_disconnection_error);
        Log.i(TAG, message);
        return b;
    }
    
    /**
     * Send an Http request by Get method for getting user connected list.
     * @param context
     * @return
     */
    public static User[] getConnectUser(final Context context) {
    	Log.i(TAG, "getting user connected...");
        boolean b = false;
        String serverUrl = SERVER_URL;
        User[] users = null;
        Node response;
        String message;
        Map<String, String> params = new HashMap<String, String>();
        params.put(PARAM_CONTROLLER, C_REGISTER);
        params.put(PARAM_ACTION, A_GET_USERS_ONLINE);
        
        response = ServerHttp.tryAConnection(serverUrl, params, context);
        
        if(response != null) {
        	if(!ParserXML.getState(response).equals("")) {
                message = context.getString(R.string.get_user_connected);
                users = ParserXML.getUsers(response);
                b = true;
            } else {
            	message = context.getString(R.string.get_user_connected_error);
            }
        	Log.i(TAG, message);
            if(b) return users;
            else return null;	
        }
     
        message = context.getString(R.string.get_user_connected_error);
        Log.i(TAG, message);
        return null;
    }
}
